package cn.iocoder.yudao.module.product.service.product;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.app.product.vo.SecondProductPageReqVO;
import cn.iocoder.yudao.module.product.controller.app.product.vo.SecondProductReqVO;
import cn.iocoder.yudao.module.product.controller.app.product.vo.SecondProductViewHistoryReqVO;
import cn.iocoder.yudao.module.product.controller.vo.SecondProductPageRespVO;
import cn.iocoder.yudao.module.product.convert.product.SecondProductConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.product.SecondProductDO;
import cn.iocoder.yudao.module.product.dal.dataobject.product.SecondProductViewHistoryDO;
import cn.iocoder.yudao.module.product.dal.mysql.product.SecondProductMapper;
import cn.iocoder.yudao.module.product.dal.mysql.product.SecondProductViewHistoryMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Date;

import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Service
@Slf4j
@Validated
public class SecondProductServiceImpl implements SecondProductService {
    @Resource
    private SecondProductMapper secondProductMapper;

    @Resource
    private SecondProductViewHistoryMapper secondProductViewHistoryMapper;

    @Override
    public Long push(SecondProductReqVO secondProductReqVO) {
        SecondProductDO secondProductDO = SecondProductConvert.INSTANCE.convert(secondProductReqVO);
        secondProductDO.setProductStatus(0);
        secondProductDO.setCreateTime(buildTime(2021, 2, 1));
        secondProductDO.setCreator(String.valueOf(getLoginUserId()));
        secondProductMapper.insert(secondProductDO);
        return secondProductDO.getId();
    }

    @Override
    public void delete(Long id, SecondProductReqVO secondProductReqVO) {
        SecondProductDO secondProductDO = SecondProductConvert.INSTANCE.convert(secondProductReqVO);
        secondProductDO.setId(id);
        secondProductDO.setUpdateTime(buildTime(2021, 2, 1));
        secondProductDO.setDeleted(true);
        secondProductMapper.updateById(secondProductDO);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        SecondProductDO secondProductDO = new SecondProductDO();
        secondProductDO.setUpdateTime(buildTime(2021, 2, 1));
        secondProductDO.setId(id);
        secondProductDO.setProductStatus(status);
        secondProductMapper.updateById(secondProductDO);
    }

    @Override
    public void addViewHistory(Long secondProductId){
        SecondProductViewHistoryDO secondProductViewHistoryDO = new SecondProductViewHistoryDO();
        secondProductViewHistoryDO.setSecondProductId(secondProductId);
        secondProductViewHistoryDO.setCreator(String.valueOf(getLoginUserId()));
        secondProductViewHistoryDO.setCreateTime(new Date());
        secondProductViewHistoryMapper.insert(secondProductViewHistoryDO);
    }

    @Override
    public PageResult<SecondProductPageRespVO> getProductPage(SecondProductPageReqVO reqVO) {
        IPage<SecondProductPageRespVO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        secondProductMapper.customPage(page,reqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public SecondProductPageRespVO detailById(SecondProductPageReqVO pageVO) {
        return secondProductMapper.customOne(pageVO);
    }

}
